﻿namespace Wiki.Web.Mvc
{
    using System;
    using System.Web.Mvc;
    using System.Web.Routing;

    public static class ControllerExtensionMethods
    {
        public static Uri RouteUri(this Controller arg, string routeName)
        {
            if (null == arg)
            {
                throw new ArgumentNullException("arg");
            }

            if (null == routeName)
            {
                throw new ArgumentNullException("routeName");
            }

            ViewContext context = new ViewContext(arg.ControllerContext, "Foo", null, null, null);
            Uri relativeUri = new Uri(new UrlHelper(context).RouteUrl(routeName), UriKind.Relative);

            return new Uri(arg.Request.Url, relativeUri);
        }

        public static Uri RouteUri(this Controller arg, string routeName, RouteValueDictionary values)
        {
            if (null == arg)
            {
                throw new ArgumentNullException("arg");
            }

            if (null == routeName)
            {
                throw new ArgumentNullException("routeName");
            }

            if (null == values)
            {
                throw new ArgumentNullException("values");
            }

            ViewContext context = new ViewContext(arg.ControllerContext, "Foo", null, null, null);
            Uri relativeUri = new Uri(new UrlHelper(context).RouteUrl(routeName, values), UriKind.Relative);

            return new Uri(arg.Request.Url, relativeUri);
        }
    }
}